﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CodeGenerator.Core;

namespace CodeGenerator.UI.BuildHControl
{
    public partial class UcBasicControl : UserControl
    {
        public UcBasicControl()
        {
            InitializeComponent();
        }

        public TableInfo tableInfo = new TableInfo();

        private void UcBasicControl_Load(object sender, EventArgs e)
        {
           
            MainSetting info = Config.GetSetupInfo();

            txtNameSpace.Text = info.NameSpace;
            txtAreaName.Text = info.AreaName;
            txtOutputDir.Text = info.OutputDir;
            txtPrefix.Text = info.Prefix;

            ServerInfo server = Constant.CurrentServerInfo;
            if (server != null && !string.IsNullOrEmpty(Constant.DatabaseName))
            {
                IDatabase database = DBFactory.Database(server.DBType);

                string tableName = Constant.TableName;
                tableInfo.Name = tableName;

                tableInfo.Comments = database.GetTableComments(server, Constant.DatabaseName, tableName);
                tableInfo.ColumnList = database.GetColumnInfoList(server, server.DatabaseName, tableName);
                foreach (var column in tableInfo.ColumnList)
                {
                    cmbPrimaryKey.Items.Add(column.Name);
                    if (column.IsPrimaryKey == "是")
                        cmbPrimaryKey.SelectedIndex = cmbPrimaryKey.Items.IndexOf(column.Name);
                }
            }
        }

        private void btnChoose_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
            folderBrowserDialog.ShowDialog();
            txtOutputDir.Text = folderBrowserDialog.SelectedPath;
        }

        public Dictionary<string,object> GetBasicData()
        {
            Dictionary<string, object> dict = new Dictionary<string, object>();
            dict.Add("NameSpace", txtNameSpace.Text.Trim());
            dict.Add("AreaName", txtAreaName.Text.Trim());
            dict.Add("OutputDir", txtOutputDir.Text.Trim());
            dict.Add("Prefix", txtPrefix.Text.Trim());
            dict.Add("PrimaryKey", cmbPrimaryKey.SelectedText);
            dict.Add("BtnAuthorization", chkBtnAuthorization.Checked);
            dict.Add("ViewAuthorization", chkViewAuthorization.Checked);

            dict.Add("Preview", chbPreview.Checked);

            return dict;
        }
    }
}
